package com.lannuokeji.biz.items.controller;


import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.lannuokeji.base.AController;
import com.lannuokeji.base.facade.PageRequest;
import com.lannuokeji.biz.items.entity.Address;
import com.lannuokeji.biz.items.entity.DeviceType;
import com.lannuokeji.biz.items.service.AddressService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * <p>
 * 设备存放地 前端控制器
 * </p>
 *
 * @author lannuoTeck
 * @since 2018-03-19
 */
@Api(description="设备存放地")
@RestController
@RequestMapping("/items/address")
public class AddressController extends AController {

	@Resource
	AddressService addressService;
	
	@ApiOperation(value = "查询列表")
	@GetMapping()
	public Object getList() {
		List<Address> l = addressService.selectList(new EntityWrapper<>());
		return setSuccessContent(l);
	}
	
	@ApiOperation(value = "查询列表")
	@GetMapping("/page")
	public Object getList(PageRequest pageRequest, Address entity) {
		Page page = new Page(pageRequest.getCurrent(), pageRequest.getSize());
		EntityWrapper<Address> entityWrapper = new EntityWrapper<>();
		entityWrapper.eq("status", 1);
		page = addressService.selectPage(page, entityWrapper);
		return setSuccessContent(page);
	}

	@ApiOperation(value = "添加地址")
	@PostMapping()
	public Object create(HttpServletRequest request, @RequestBody Address entity) {
		boolean ret = addressService.insertAllColumn(entity);
		if (ret) {
			return setSuccessContent("添加成功！");
		} else {
			return setErrorContent("添加失败！");
		}
	}

	@ApiOperation(value = "根据id查询地址")
	@GetMapping("/{id}")
	public Object get(HttpServletRequest request,
			@ApiParam(value = "地址id", required = true) @PathVariable Integer id) {
		Address items = addressService.selectById(id);
		return setSuccessContent(items);
	}

	@ApiOperation(value = "删除地址信息")
	@DeleteMapping("/{id}")
	public Object del(HttpServletRequest request,
			@ApiParam(value = "地址id", required = true) @PathVariable Integer id) {
		if (addressService.deleteById(id)) {
			return setSuccessContent("删除成功");
		} else {
			return setErrorContent("删除失败");
		}
	}

	@ApiOperation(value = "修改地址信息")
	@PutMapping()
	public Object update(HttpServletRequest request, @RequestBody Address entity) {
		if (addressService.updateById(entity)) {
			return setSuccessContent("修改成功！");
		} else {
			return setErrorContent("修改失败！");
		}
	}
}

